#include <stdio.h>
#include "gui_form.h"
#include "tskcfg.h"
#include "bsp.h"
#include "sensor.h"
#include "lora.h"

static void update(uint8_t keyvalue);

char temp[12];

const GUI_FormType Gui_FrmMainmenu = 
{
	.draw    = NULL,
	.load    = NULL,
	.unload  = NULL,
	.update  = update,
	.keys    = NULL,
  .refresh = 500,
};

static void update(uint8_t keyvalue)
{
  int16_t pres;
  uint16_t upres;
  
  Lcd_ClearFb();
  
  pres = SENSOR.pressure;
  upres = abs(SENSOR.pressure);
  
  if((pres >= 0) && (pres < 10))             /* 1位 */
  {
    temp[0] = pres + 0x30;
    temp[1] = 0;
    gui_ascs32(52, 0, temp,  1);
  }
  else if((pres >= 10) && (pres < 100))      /* +2位 */
  {
    temp[0] = (pres / 10) + 0x30;
    temp[1] = (pres % 10) + 0x30;
    temp[2] = 0;
    gui_ascs32(40, 0, temp,  1);
  }
  else if((pres <= -1) && (pres > -10))      /* -2位 */
  {
    temp[0] = '-';
    temp[1] = upres + 0x30;
    temp[2] = 0;
    gui_ascs32(40, 0, temp,  1);
  }
  else if((pres >= 100) && (pres < 1000))    /* +3位 */
  {
    temp[0] = (pres / 100) + 0x30;
    temp[1] = ((pres / 10) % 10) + 0x30;
    temp[2] = (pres % 10) + 0x30;
    temp[3] = 0;
    gui_ascs32(28, 0, temp,  1);
  }
  else if((pres <= -10) && (pres > -100))    /* -3位 */
  {
    temp[0] = '-';
    temp[1] = (upres / 10) + 0x30;
    temp[2] = (upres % 10) + 0x30;
    temp[3] = 0;
    gui_ascs32(28, 0, temp,  1);
  }
  else if((pres <= -100) && (pres > -1000))  /* -4位 */
  {
    temp[0] = '-';
    temp[1] = (upres / 100) + 0x30;
    temp[2] = ((upres / 10) % 10) + 0x30;
    temp[3] = (upres % 10) + 0x30;
    temp[4] = 0;
    gui_ascs32(14, 0, temp,  1);
  }
  else
  {
    temp[0] = '-';
    temp[1] = '-';
    temp[2] = '-';
    temp[3] = 0;
    gui_ascs32(28, 0, temp,  1);
  }
  
  gui_ascs16(112, 30, "Pa", 1);
  
  gui_base_rectangle(0,48, 128, 16);
  
  gui_base_fillrect(1, 49, upres/4, 14);
  
  if(LoRa_ConnectFlag == 1)
  {
    gui_ascs16(0, 0, "}~", 1);
  }
  
//  snprintf(temp, 12, "{|:%.1f", SENSOR.temperature);
//  gui_ascs16(0, 48, temp,  1);
//  
//  snprintf(temp, 12, "}~:%.1f", SENSOR.humidity);
//  gui_ascs16(72, 48, temp,  1); 
}
